package com.nine.system.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nine.common.pojo.User;
import com.nine.system.dao.UserDao;
import com.nine.system.service.UserService;
import com.nine.system.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @program: nine-education-parent
 * @author: ♥丁新华
 * @create: 2023-05-22 23:46
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired

    private UserDao userDao;

    /**
     * 查找账号密码
     * @param username
     * @param password
     * @return
     */
    @Override
    public User findByNameAndPwd(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<> ();
        wrapper.eq("username",username);
        //把输入的密码加盐
        //使用hutool 工具
        wrapper.eq("password", DigestUtil.sha256Hex(password));
        //System.out.println ("++++++" + DigestUtil.sha256Hex(password));
        //wrapper.eq ("password",password);
        User user = (User) userDao.selectOne(wrapper);
        return user;
    }

    /**
     * 分  页
     * @param current
     * @param pageSize
     * @param userVo
     * @return
     */
    @Override
    public Page<User> findUserPage(Integer current, Integer pageSize, UserVo userVo) {
        //分 页
        Page<User> page = new Page<> (current, pageSize);

        //模糊查询
        QueryWrapper<User> wrapper = new QueryWrapper<> ();

        //判 断 一 下
        if (StringUtils.hasText (userVo.getRealName ())){
            //todo 可能要改
            wrapper.like ("nick_name",userVo.getRealName ());
        }
        Page<User> userPage = userDao.selectPage (page, wrapper);

        return userPage;
    }

    /**
     * 修 改 状 态
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateStatus(String id, Integer status) {
        User user = new User ();
        user.setId (id);
        user.setIsDeleted (status);

        user.setGmtModified (LocalDateTime.now ());//最新时间
        int i = userDao.updateById (user);

        return i;
    }
}
